/*
 * @Author: Ma Xiaohan
 * @Date: 2018-08-19 15:58:06
 * @Last Modified by: Xiaohan Ma
 * @Last Modified time: 2018-08-31 18:30:38
 */
const fs = require('fs');
const path = require('path');
const rename = think.promisify(fs.rename, fs);
const assert = require('assert');
const BasicController = require('../BasicController.js');

module.exports = class extends BasicController {
  constructor(ctx) {
    super(ctx);
    assert(think.isFunction(this.model), 'this.model must be a function');
    this.MODEL = this.ctx.state.ApiInfo.model;
    this.params = {
      model: this.MODEL,
      data: this.post()
    };
  }

  //  上传项目图片或文件
  async uploadfilesAction() {
    try {
      const params = this.params;
      const file = this.file();
      const fileKeys = Object.getOwnPropertyNames(file);

      if (think.isEmpty(fileKeys)) {
        return this.fail('上传失败,图片不能为空');
      }
      // 批量上传成功返回id数组   未启用
      // const backdata = [];
      for (const i of fileKeys) {
        let files = [];
        // 单张图片接收到的file 参数为object，多图片为array，统一将其转化为array
        if (think.isObject(file[i])) {
          files.push(file[i]);
        } else if (think.isArray(file[i])) {
          files = [...file[i]];
        }
        for (const item of files) {
          const fileid = think.uuid('v4');
          let name = path.extname(item.name);
          // name = `${think.md5(item.name)}${name}`;
          name = `${fileid}${name}`;
          const filepath = path.join(think.ROOT_PATH, `runtime/upload/recruit_enclosure/${params.data.relation_id}/${name}`);
          think.mkdir(path.dirname(filepath));
          await rename(item.path, filepath);
          const filedata = {};
          filedata.id = fileid;
          filedata.file_path = filepath;
          filedata.relation_id = params.data.relation_id;
          params.filedata = filedata;
          await this.model(params.model).uploadfilesAction(params);
          // 未启用
          // backdata.push(fileid);
          return this.success({id: fileid}, '上传成功');
        }
      }

      // const backdata = await this.model(params.model).getFilesListByIdAction(params);
      // return this.success({id: backdata}, '上传成功');
    } catch (error) {
      return this.fail(error, '上传失败');
    }
  };

  async downloadAction() {
    const {
      filepath
    } = this.post();
    return this.ctx.download(filepath);
  }
};

/**
 * @api {POST} /recruit_enclosure/recruit_enclosure/uploadfiles /uploadfiles
 * @apiDescription 招新文件上传
 * @apiGroup recruit_enclosure
 *
 * @apiParam {String} relation_id 招新信息id
 *
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *{
 *   "code": 1001,
 *   "msg": {
 *      "relation_id": "relation_id can not be blank"
 *   }
 *}
 *{
 *   "code": 0,
 *  "msg": "上传失败,文件不能为空"
 *}
 * @apiSuccessExample {json} 正确返回值:
 *    {
 *   "code": 0,
 *     "msg": [
 *      {
 *           "id": "028b88a7-f144-49f2-8ec9-c4bd603b7b29",
 *          "file_path": "/home/mdalang/projects/mobile_internet_website_server/runtime/upload/projectImgInfo/undefined/20180729-161755.png",
 *          "relation_id": "555",
 *          "file_upload_time":"2018-08-11 09:37:19"
 *      },
 *      {
 *          "id": "348e5efa-1d56-4956-a196-d16822d709f9",
 *          "file_path": "/home/mdalang/projects/mobile_internet_website_server/runtime/upload/projectImgInfo/undefined/20180729-161755.png",
 *          "relation_id": "555",
 *          "file_upload_time":"2018-08-11 09:37:19"
 *      }
 *  ],
 *  "data": "上传成功"
 * }
 */

/**
 * @api {post} /recruit_enclosure/recruit_enclosure/download /download
 * @apiDescription 下载文件
 * @apiGroup recruit_enclosure
 * @apiParam {String} filepath 文件url
 *
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 * {
 *  "code": 1001,
 *  "msg": {
 *      "filepath": "filepath can not be blank"
 *  }
 * }
 * @apiSuccessExample {json} 正确返回值:
 * 文件
 */

/**
 * @api {delete} /recruit_enclosure/recruit_enclosure/del /del
 * @apiDescription 根据id删除附件
 * @apiGroup recruit_enclosure
 *
 * @apiParam {String} id 附件ID
 *
 *
 * @apiParamExample {object} 请求参数格式:
 *  {
 * id: dda33412-8e95-40f9-92f5-f01df8dd2d21,
 * }
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 * {
 * "code": 1001,
 * "msg": {
 *      "id": "id can not be blank"
 *  }
 * }
 * @apiSuccessExample {json} 正确返回值:
 * {
 *  "code": 0,
 *  "msg": "删除成功",
 *  "data": 0
 * }
 */

/**
 * @api {get} /recruit_enclosure/recruit_enclosure/get /get
 * @apiDescription 根据id查询单个附件信息
 * @apiGroup recruit_enclosure
 * @apiParam {String} id  附件ID
 *
 *
 * @apiParamExample {string} 请求参数格式:
 *    ?id=8b3a12bd-6195-403b-9fcc-a6a053d6d073
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *{
 *   "code": 1001,
 *   "msg": {
 *      "id": "id can not be blank"
 *   }
 *}
 * @apiSuccessExample {json} 正确返回值:
{
    "code": 0,
    "msg": "",
    "data": {
        "id": "36ddc25e-4992-47a1-92f7-b8bba4a02c0e",
        "file_path": "/home/mdalang/projects/mobile_internet_website_server/runtime/upload/recruit_enclosure/2453453/20180729-161733.png",
        "relation_id": "2453453",
        "file_upload_time": "2018-08-11 11:17:02"
    }
}
 */

/**
 * @api {POST} /recruit_enclosure/recruit_enclosure/listpage /listpage
 * @apiDescription 获取全部项目信息列表/根据条件获取项目信息列表
 * @apiGroup recruit_enclosure
 *
 * @apiParam {String} [id] 文件ID
 * @apiParam {String} [file_path] 文件路径
 * @apiParam {String} [relation_id] 文件所属招新信息id
 * @apiParam {String} [project_technology] 项目所用技术
 * @apiParam {String} [file_upload_time] 文件上传时间
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *     {
 *        "code": ,
 *        "msg": "获取失败",
 *        "error": {
 *        },
 *     }
 *
 * @apiSuccessExample {json} 正确返回值:
{
    "code": 0,
    "msg": "",
    "data": {
        "pageSize": 20,
        "current": 1,
        "pages": 1,
        "total": 2,
        "data": [
            {
                "id": "36ddc25e-4992-47a1-92f7-b8bba4a02c0e",
                "file_path": "/home/mdalang/projects/mobile_internet_website_server/runtime/upload/recruit_enclosure/2453453/20180729-161733.png",
                "relation_id": "2453453",
                "file_upload_time": "2018-08-11 11:17:02"
            },
            {
                "id": "acdf4874-bf96-4f3a-9810-6fbbcda76cf1",
                "file_path": "/home/mdalang/projects/mobile_internet_website_server/runtime/upload/recruit_enclosure/2453453/20180729-161733.png",
                "relation_id": "2453453",
                "file_upload_time": "2018-08-11 11:18:12"
            }
        ]
    }
}
 */
